package org.yjw;

import java.util.*;

/**
 * @author 31898
 */
public class TestHashMap {
//    public static void main(String[] args) {
//        HashMap<Integer, User> users = new HashMap<>();
//        users.put(1, new User("张三", 25));
//        users.put(3, new User("李四", 22));
//        users.put(2, new User("王五", 28));
//        List list = new ArrayList();
//        System.out.println(users);
//        HashMap<Integer, User> sortHashMap = sortHashMap(users);
//        System.out.println(sortHashMap);
//    }

    public static void main(String[] args) {

    }

    /**
     * 用两个队列模拟栈内存先进后出的模式
     * 队列先进先出
     */
    public void queue() {
        //a 队列
        Queue<String> queue = new LinkedList<String>();
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        queue.offer("d");
        queue.offer("e");
        //a 队列依次加入 list 集合之中
        //arrylist 集合是中间参数
        ArrayList<String> a = new ArrayList<String>();
        for (String q : queue) {
            a.add(q);
            System.out.print(q);
        }
        System.out.print("进栈：");
        //b 队列
        Queue<String> queue2 = new LinkedList<String>();
        //以倒序的方法取出（a 队列依次加入 list 集合）之中的值，加入 b 对列
        for (int i = a.size() - 1; i >= 0; i--) {
            queue2.offer(a.get(i));
        }
        //打印出栈队列
        System.out.print("出栈：");
        for (String q : queue2) {
            System.out.print(q);
        }
    }

    /**
     * HashMap值排序返回LinkHashMap
     *
     * @param userHashMap
     * @return
     */
    public static HashMap<Integer, User> sortHashMap(HashMap<Integer, User> userHashMap) {
        Set<Map.Entry<Integer, User>> entrySet = userHashMap.entrySet();
        List<Map.Entry<Integer, User>> list = new ArrayList<>(entrySet);
        Collections.sort(list, new Comparator<Map.Entry<Integer, User>>() {
            @Override
            public int compare(Map.Entry<Integer, User> o1, Map.Entry<Integer, User> o2) {
                return o1.getValue().getAge() - o2.getValue().getAge();
            }
        });
        HashMap hashMap = new LinkedHashMap();
        for (Map.Entry<Integer, User> entry : list) {
            hashMap.put(entry.getKey(), entry.getValue());
        }
        return hashMap;
    }

}

class User {
    private String name = "张山";
    private Integer age = 16;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}